package com.future.bantang.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.Toast;

import com.future.bantang.R;
import com.future.bantang.adapter.FragmentSearchInventoryGridAdapter;
import com.future.bantang.constants.BanTang;
import com.future.bantang.constants.URL;
import com.future.bantang.entity.SearchCategorySceneEntity;
import com.future.bantang.utils.JsonUtils;
import com.future.bantang.utils.NetUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.view.ResType;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ResInject;
import com.lidroid.xutils.view.annotation.ViewInject;

import java.util.List;

/**
 * Created by zzh on 2016/1/16.
 * 搜索分类-->清单
 */

public class SearchInventoryFragment extends BaseFragment{

    @ViewInject(R.id.grid_fra_search_inventory_list)
    private GridView gridInventory;

    @ResInject(id = R.string.network_error,type = ResType.String)
    private String networkErro;

    private String inventoryUrl;
    private FragmentSearchInventoryGridAdapter gridAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_search_inventory, container, false);
    }

    @Override
    protected void initView(View view) {
        inventoryUrl= URL.CATEGORY_SCENE_URL;
        gridAdapter = new FragmentSearchInventoryGridAdapter(getActivity());
        gridInventory.setAdapter(gridAdapter);

    }

    @Override
    protected void loadDatas() {
        NetUtils.RequestParamsBuilder builder=new NetUtils.RequestParamsBuilder();

        /*
        client_id=bt_app_android&client_secret=ffcda7a1c4ff338e05c42e7972ba7b8d
         */
        RequestParams params = builder.addPostParameter(BanTang.CLIENT_ID_KEY, BanTang.CLIENT_ID_VALUE)
                .addPostParameter(BanTang.CLIENT_SECRET_KEY, BanTang.CLIENT_SECRET_VALUE).build();

        NetUtils.doPost(inventoryUrl, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                try {
                    List<SearchCategorySceneEntity.DataEntity> dataEntities = JsonUtils.perse(responseInfo.result, SearchCategorySceneEntity.class).getData();

                    if (dataEntities.size()>0) {
                        gridAdapter.setDatas(dataEntities);
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(HttpException error, String msg) {
                Toast.makeText(getActivity(), networkErro, Toast.LENGTH_SHORT).show();
            }
        });

    }
}
